<!--
 * @Description: 
 * @Author: Leequik
 * @Date: 2025-06-15 14:58:30
 * @LastEditTime: 2025-07-07 11:31:37
 * @LastEditors: Leequik
-->
<template>
    <div class="child">
        <h3>子组件2</h3>
        <h5>电脑：{{ computer}}</h5>
         <el-button @click="emitter.emit('send-com',computer)">电脑给父亲</el-button>
        <h5 v-show="toy">哥哥给的玩具：{{ toy}}</h5>
    </div>
</template>
<script lang="ts" setup>
import { ref,onUnmounted } from 'vue';
import emitter from '../../utils/emitter.ts';

let computer = ref('苹果');
let toy = ref('');

//给emitter绑定send-com事件
emitter.on('send-toy', (data) => {
    console.log('子组件2接收到1的数据：', data);
    toy.value = data;
});

//组件卸载时，解绑事件
onUnmounted(() => {
    emitter.off('send-toy');
});

</script>
<style scoped>
.child {
    background-color: pink;
    padding: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    border-radius: 10px;
    margin: 10px;
}
</style>